package com.samson.elasticsearch.controller;

import com.google.gson.Gson;
import com.samson.elasticsearch.dao.EmployeeDao;
import com.samson.elasticsearch.entity.EmployeeEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author samson
 * @version 1.0
 * @date 2019/7/3 12:02
 */
@RestController
@RequestMapping("/es_test")
public class ElasticSearchController {

    @Autowired
    private EmployeeDao er;

    //增加
    @RequestMapping("/add")
    public String add(){

        EmployeeEntity employeeEntity =new EmployeeEntity();
        employeeEntity.setId("1");
        employeeEntity.setFirstName("xuxu");
        employeeEntity.setLastName("zh");
        employeeEntity.setAge(26);
        employeeEntity.setAbout("i am in peking");
        er.save(employeeEntity);
        System.err.println("add a obj");

        return "success";
    }
    @RequestMapping("/add2")
    public String add2(){

        EmployeeEntity employeeEntity =new EmployeeEntity();
        employeeEntity.setId("2");
        employeeEntity.setFirstName("xuxu");
        employeeEntity.setLastName("zh");
        employeeEntity.setAge(26);
        employeeEntity.setAbout("i am in peking");
        er.save(employeeEntity);
        System.err.println("add a obj");

        return "success";
    }
    @RequestMapping("/add3")
    public String add3(){

        EmployeeEntity employeeEntity =new EmployeeEntity();
        employeeEntity.setId("3");
        employeeEntity.setFirstName("xuxu");
        employeeEntity.setLastName("zh");
        employeeEntity.setAge(26);
        employeeEntity.setAbout("i am in peking");
        er.save(employeeEntity);
        System.err.println("add a obj");

        return "success";
    }

    //删除
    @RequestMapping("/delete")
    public String delete(){
        EmployeeEntity accountInfo=er.queryEmployeeById("1");
        er.delete(accountInfo);
        return "success";
    }

    //局部更新
    @RequestMapping("/update")
    public String update(){

        EmployeeEntity employeeEntity =er.queryEmployeeById("1");
        employeeEntity.setFirstName("哈哈");
        er.save(employeeEntity);

        System.err.println("update a obj");

        return "success";
    }

    //查询
    @RequestMapping("/query")
    public EmployeeEntity query(){

        EmployeeEntity accountInfo=er.queryEmployeeById("1");
        System.err.println(new Gson().toJson(accountInfo));

        return accountInfo;
    }

}
